package com.km.team.phsystem.service.impl;

import com.km.team.phsystem.beans.pojo.SystemUser;
import com.km.team.phsystem.beans.pojo.SystemUserExample;
import com.km.team.phsystem.beans.vo.PageBean;
import com.km.team.phsystem.beans.vo.SystemUserVo;
import com.km.team.phsystem.common.utils.StrUtils;
import com.km.team.phsystem.dao.mybatis.SystemUserMapper;
import com.km.team.phsystem.dao.mybatis.custom.SystemUserMapperCustom;
import com.km.team.phsystem.service.ISystemUserService;
import com.km.team.phsystem.service.impl.base.BaseService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.security.MessageDigest;
import java.util.List;

/**
 * Author: jiangjun
 * Date: 2018/10/14
 * Time: 下午4:08
 * Description:
 */
@Service("systemUserService")
public class SystemUserServiceImpl extends BaseService<SystemUserServiceImpl, SystemUser> implements ISystemUserService {

    @Autowired
    private SystemUserMapperCustom mapperCustom;

    @Autowired
    private SystemUserMapper mapper;

    @Transactional
    @Override
    public int update(SystemUser systemUser) throws Exception {
        if(systemUser == null){
            return 0;
        }
        super.baseUpdate(systemUser);
        throw new RuntimeException();
    }

    @Override
    public int add(SystemUserVo vo) throws Exception {
        SystemUser systemUser=new SystemUser();
        BeanUtils.copyProperties(vo,systemUser);
        systemUser.setPassword(StrUtils.getMD5(systemUser.getPassword()));
        return mapper.insert(systemUser);
    }

    @Override
    public List<SystemUser> getUser(SystemUserExample systemUserExample) throws Exception {
        return mapper.selectByExample(systemUserExample);
    }

    @Override
    public PageBean getPagedData(SystemUserVo vo) throws Exception {
        // 查询分页数据
        calculateStartIndex(vo);
        List<SystemUser> recordList = mapperCustom.getPagedData(vo);
        Integer recordCount = mapperCustom.getRecordCount(vo);
        return new PageBean(vo.getPageBean().getCurrentPage(), vo.getPageBean().getPageSize(), recordCount, recordList);
    }
}
